<?php
namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;
class OssSet extends Base
{
    public function _initialize()
    {
        parent::_initialize();
        //  更新插件
        //$this->insertPlugin($this->scanPlugin());
    }
    public function index(){
        //$map = $this->_search();

        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            $count = Db::name('oss_set')->alias('p')->where($map)->count();

            $field = 'p.*';
            $list = Db::name('oss_set')
                ->alias('p')
                ->field($field)
                ->where($map)
                ->order('p.id desc ')
                ->page($page,$limit)
                ->select()->toArray();
            foreach($list as $k=>$v){
                $list[$k]['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
            }
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
        }

        return View::fetch();
    }

    public function edit(){
        if(IS_POST){
            $params = input();
            Db::startTrans();
            $insert = array();
            $insert['name'] = $params['name'];
            $insert['status'] = $params['status'];
            $insert['config'] = json_encode($params['config']);
            $insert['w_time'] = getCurTime();
            $insert['admin_id'] = session('admin_id');
            if(input('id')){
                $rs = Db::name('oss_set')->where('id',$params['id'])->update($insert);
            }else{
                $rs = Db::name('oss_set')->insert($insert);
            }
            if(!$rs){
                Db::rollback();
                return json(['status'=>0,'msg'=>'更新失败']);
            }
            Db::commit();
            return json(['status'=>1,'msg'=>'修改成功']);
        }else{
            $id = input('id');
            $info = Db::name('oss_set')->where('id',$id)->find();
            View::assign('info',$info);
            $config = json_decode($info['config'],true);
            View::assign('config',$config);
            return View::fetch();
        }
    }

    public function del(){
        $id_arr = input('ids/a');
        Db::startTrans();
        Db::name('level_log')->where(['id'=>['in',$id_arr]])->delete();
        Db::commit();
        return json(['status'=>1,'msg'=>'删除成功!']);
    }
    /**
     * 插件目录扫描
     * @return array 返回目录数组
     */
    private function scanPlugin(){
        $plugin_list['payment'] = $this->dirscan(C('PAYMENT_PLUGIN_PATH'));
        foreach($plugin_list as $k=>$v){
            foreach($v as $k2=>$v2){

                if(!file_exists(PLUGIN_PATH.$k.'/'.$v2.'/config.php'))
                    unset($plugin_list[$k][$k2]);
                else
                {
                    $plugin_list[$k][$v2] = include(PLUGIN_PATH.$k.'/'.$v2.'/config.php');
                    unset($plugin_list[$k][$k2]);
                }
            }
        }
        return $plugin_list;
    }

    /**
     * 获取插件目录列表
     * @param $dir
     * @return array
     */
    private function dirscan($dir){
        $dirArray = array();
        if (false != ($handle = opendir ( $dir ))) {
            $i=0;
            while ( false !== ($file = readdir ( $handle )) ) {
                //去掉"“.”、“..”以及带“.xxx”后缀的文件
                if ($file != "." && $file != ".."&&!strpos($file,".")) {
                    $dirArray[$i]=$file;
                    $i++;
                }
            }
            //关闭句柄
            closedir ( $handle );
        }
        return $dirArray;
    }

    /**
     * 更新插件到数据库
     * @param $plugin_list array 本地插件数组
     */
    private function insertPlugin($plugin_list){
        //插件类型
        foreach($plugin_list as $pt=>$pv){
            //  本地对比数据库
            foreach($pv as $t=>$v){
                // 对比数据库 本地有 数据库没有
                $is_exit = Db::name('plugin')->where(array('type'=>$pt,'code'=>$v['code']))->find();
                if(empty($is_exit)){
                    $add['icon'] = $v['icon'];
                    $add['code'] = $v['code'];
                    $add['name'] = $v['name'];
                    $add['version'] = $v['version'];
                    $add['author'] = $v['author'];
                    $add['desc'] = $v['desc'];
                    $add['bank_code'] = serialize($v['bank_code']);
                    $add['type'] = $pt;
                    $add['scene'] = $v['scene'];
                    $add['config'] = empty($v['config']) ? '' : serialize($v['config']);
                    $add['userset'] = $v['userset'];
                    Db::name('plugin')->add($add);
                }
            }
        }
    }
}
